grErp.filter('getarr',function(){
    return function(obj,propty){
        var arr = [];
        angular.forEach(obj,function(data,index,array){
            if(data.belong_to == propty){
                arr.push(data);
            }
        })
        return arr;
    }
});
grErp.controller('ProCtrl',['$scope','$filter','MaterialFactory',function($scope,$filter,MaterialFactory){
    $scope.subpro=[];
    $scope.init=function(specLike){
        loadingStart();
        $scope.subpro=[{subproNum:0}];
        $scope.product={
            spec:specLike
        }
        MaterialFactory.ajaxGetClassify().then(function(d){
            $scope.classifies=d;
            loadingEnd();
        })
        MaterialFactory.ajaxGetFruitNameWithFruitClassify().then(function(d){
           $scope.furitNames=d;
        })

    }
    /**
     * 大类选择事件
     * @param $index
     */
    $scope.classifySelect = function($index){
        var classify = $scope.classifies[$index];
        $scope.product.classify = classify.classify;
        $scope.product.classify_no = classify.classify_no;
        if($scope.product.classify_no=='2'){
            loadingStart();
            MaterialFactory.ajaxgGetSubproInventory().then(function(d){
                $scope.subproductList=d;
                console.log($scope.subproductList)
                loadingEnd();
            })
        }
        $scope.autoSpecNo();
    }
    /**
     * 水果名选择
     * @param $index
     * @returns {boolean}
     */
    $scope.fruitNameSelect = function($index){
        if(parseInt($index)<0){
            $scope.pname = "";
            $scope.pname_no = "";
            return false;
        }
        var p = $scope.furitNames[$index];
        $scope.product.pname = p.pname;
        $scope.product.pname_no = p.pname_no;
        $scope.product.fruit_classify = p.fruit_classify;
        $scope.product.fruit_classify_no = p.fruit_classify_no;
        $scope.autoSpecNo();

        MaterialFactory.ajaxGetYbzSpec('1'+p.fruit_classify_no+p.pname_no).then(function(d){
            var ybzUnitArr=[];
            angular.forEach(d,function(data,index){
                var unit=$filter('getSpecUnit')(data);
                if(ybzUnitArr.indexOf(unit+"/箱")<0){
                    ybzUnitArr.push(unit+"/箱");
                    ybzUnitArr.push(unit+"/装");
                    ybzUnitArr.push(unit+"/袋");
                }
            });
            $scope.recommentUnitArr=ybzUnitArr;
        });
    }
    /**
     * 自动获取规格
     */
    $scope.autoSpecNo=function(){
        if($filter('isValidValue')($scope.product.pname_no)&&$filter('isValidValue')($scope.product.fruit_classify_no)&&$filter('isValidValue')($scope.product.classify_no)){
            var BIN=String($scope.product.classify_no)+String($scope.product.fruit_classify_no)+String($scope.product.pname_no);
            MaterialFactory.ajaxAutoGetSpecNo(BIN).then(function(d){
                $scope.product.spec_no=d;
            })
        }
    }
    /**
     * 规格补全
     * @param unit
     */
    $scope.unitclick= function (unit) {
        $scope.product.spec=String($filter('strToNum')($scope.product.spec)||"")+unit;
    }
    $scope.laterunitclick=function(unit){
        $scope.product.spec=String($filter('getSpecUnitWithNum')($scope.product.spec))+"/"+unit;
    }
    $scope.moreSubpro=function(){
        $scope.subpro.push({subproNum:0});
    }
    $scope.removeSubpro=function(index){
        if(confirm("确定删除改消耗品吗?")){
            $scope.subpro.splice(index,1);
        }
    }
    $scope.createbin = function(){
        $scope.BIN = String($scope.product.classify_no)+String($scope.product.fruit_classify_no)+String($scope.product.pname_no);
    }
    $scope.editinit=function(BIN){
        MaterialFactory.ajaxGetEditProduct(BIN).then(function(d){
            $scope.product=d;
        });
        MaterialFactory.ajaxGetProductSubpro(BIN).then(function(d){
            $scope.oldsubpro=d;
        });
        MaterialFactory.ajaxgGetSubproInventory().then(function(d){
            $scope.subproductList=d;
            console.log(d);
        })
    }
    $scope.removeOldSubpro= function (index) {
        if(confirm("确定删除改消耗品吗?")){
            $scope.oldsubpro.splice(index,1);
        }
    }
}]);
grErp.factory('MaterialFactory',['$http','$q',function($http,$q){
    var service={};
    /**
     * ajax获取货别
     * @returns {*}
     */
    service.ajaxGetClassify=function(){
        var deferred=$q.defer();
        $http({
            method:'POST',
            url:webroot+"Admin/Material/ajaxGetClassify",
        }).success(function(data,status,headers,config){
            deferred.resolve(data);
        }).error(function(data,status,headers,config){
            deferred.reject(data);
        });
        return deferred.promise;
    }
    /**
     * ajax获取自动规格编码
     * @param BIN
     * @returns {*}
     */
    service.ajaxAutoGetSpecNo=function(BIN){
        var deferred=$q.defer();
        $http({
            method:'POST',
            url:webroot+"Admin/Material/ajaxAutoGetSpecNo",
            params:{BIN:BIN}
        }).success(function(data,status,headers,config){
            deferred.resolve(data);
        }).error(function(data,status,headers,config){
            deferred.reject(data);
        });
        return deferred.promise;
    }
    /**
     * ajax获取水果名和水果分类
     * @returns {*}
     */
    service.ajaxGetFruitNameWithFruitClassify=function(){
        var deferred=$q.defer();
        $http({
            method:'POST',
            url:webroot+"Admin/Material/ajaxGetFruitNameWithFruitClassify",
        }).success(function(data,status,headers,config){
            deferred.resolve(data);
        }).error(function(data,status,headers,config){
            deferred.reject(data);
        });
        return deferred.promise;
    }
    /**
     * ajax获取原包装规格
     * @returns {*}
     */
    service.ajaxGetYbzSpec=function(BIN){
        var deferred=$q.defer();
        $http({
            method:'POST',
            url:webroot+"Admin/Material/ajaxGetYbzSpec",
            params:{BIN:BIN}
        }).success(function(data,status,headers,config){
            deferred.resolve(data);
        }).error(function(data,status,headers,config){
            deferred.reject(data);
        });
        return deferred.promise;
    }
    /**
     * ajax获取消耗品列表
     * @returns {*}
     */
    service.ajaxgGetSubproInventory=function(){
        var deferred=$q.defer();
        $http({
            method:'POST',
            url:webroot+"Admin/Subpro/ajaxGetSubproProductList",
        }).success(function(data,status,headers,config){
            deferred.resolve(data);
        }).error(function(data,status,headers,config){
            deferred.reject(data);
        });
        return deferred.promise;
    }
    /**
     * ajax获取编辑商品的信息
     * @param pid
     * @returns {*}
     */
    service.ajaxGetEditProduct=function(BIN){
        var q=$q.defer();
        $http({
            method:'POST',
            url:webroot+"Admin/material/ajaxGetEditProduct",
            params:{BIN:BIN}
        }).success(function (data) {
            q.resolve(data);
        }).error(function(err){
            q.reject(err);
        });
        return q.promise;
    }
    service.ajaxGetProductSubpro=function(BIN){
        var q=$q.defer();
        $http({
            method:'POST',
            url:webroot+"Admin/material/ajaxGetProductSubpro",
            params:{BIN:BIN}
        }).success(function (data) {
            q.resolve(data);
        }).error(function(err){
            q.reject(err);
        });
        return q.promise;
    }
    return service;
}])